﻿Public Class objInfoLogin
    Public Username As String
    Public Password As String
    Public Fullname As String
    Public FacebookID As Long
    Public AccessToken As String = "CAACEdEose0cBAKZC7KblpSQu4DtuYnSSGkd2Sy7UbVcJiDgZBaaBkTqdLCLOiEIYDpMSrHNJ1SqMF1VnOo57l1Xp3SZCKOZAJjEOZAWfO8rJ1OIrqW2tcPpTuZAbkzk0D2K9hMNfYLI6VHdAPs0p9Hje3fxhkXEPMUURNAgUCfWp1Xx84jHwYALxfsuXwDg7ikzeZBYcD5oIBTs7gi7i7Oq"
End Class
Public Class clsAccountLogin
    Dim timCheck As Timer
    Dim SecondWait As Integer = 0
    Dim web As WebBrowser
    Dim step_action As String
    Public infoLogin As New objInfoLogin
    Dim ParseHTML As New clsParseHTML
    Dim is_login = False
    Dim TimeoutLogin As Integer = 30


    Public Sub New()
        timCheck = New Timer
        timCheck.Enabled = True
        timCheck.Interval = 1000
        AddHandler timCheck.Tick, AddressOf timCheck_Tick
        web = New WebBrowser
        AddHandler web.DocumentCompleted, AddressOf web_DocumentCompleted
    End Sub

    Public Sub Login(ByVal Username As String, ByVal Password As String)
        is_login = True
        infoLogin.Username = Username
        infoLogin.Password = Password
        OpenSiteLogin()
    End Sub

    Public Sub Logout()
        infoLogin = New objInfoLogin
        frmMain.ResetForm()
        web.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())")
    End Sub

    Private Sub LoginSuccess()
        is_login = False
        frmMain.panelLogin.Height = 0
        frmMain.lblFullname.Text = infoLogin.Fullname
        frmMain.lblFBID.Text = infoLogin.FacebookID
        frmMain.picAvatar.ImageLocation = "http://graph.facebook.com/" & infoLogin.FacebookID & "/picture?type=square"
        frmMain.panelLoged.Height = 106

        frmMain.FriendFB.UpdateFriendCount()
        frmMain.Account.AccessToken.getAccessToken()
    End Sub

    Private Sub LoginFail()
        is_login = False
        Logout()
        MsgBox("Đăng nhập thất bại", MsgBoxStyle.Critical, "Facebook Master")
    End Sub

    Private Sub OpenSiteLogin()
        step_action = "open"
        web.Navigate("http://m.facebook.com/me", False)
    End Sub

    Private Sub Timeout()
        LoginFail()
        web.Navigate("about:blank")
        step_action = ""
    End Sub

    Private Sub timCheck_Tick()
        If (is_login = True) Then
            SecondWait = SecondWait + 1
            If SecondWait > TimeoutLogin Then
                is_login = False
                Timeout()
            End If
        End If
    End Sub

    Private Sub web_DocumentCompleted()
        If (frmMain.is_debug = True) Then
            frmDebug.web_debug_login.Navigate("about:blank")
            frmDebug.web_debug_login.Document.Write("<html><head><title></title><body>" & web.DocumentText & "</body></html>")
            frmDebug.txt_debug_login.Text = web.DocumentText
        End If
        Dim t As ArrayList
        t = ParseHTML.getByClassname(web.Document.Body.InnerHtml, "strong", "bg", True)
        If (t.Count > 0) Then
            infoLogin.Fullname = t(0)
            t = ParseHTML.getByName(web.Document.Body.InnerHtml, "input", "target", "value")
            If (t.Count > 0) Then
                infoLogin.FacebookID = t(0)
            End If
        End If
        If (infoLogin.Fullname = "") Then
            If (step_action = "open") Then
                Try
                    If (ParseHTML.CheckByName(web, "input", "email") = True) Then
                        ParseHTML.SetByName(web, "input", "email", "value", infoLogin.Username)
                        ParseHTML.SetByName(web, "input", "pass", "value", infoLogin.Password)
                        ParseHTML.SetSubmitById(web, "login_form")
                        step_action = "check_login"
                    Else
                        'Debug.Print("sai pass")
                    End If
                Catch ex As Exception
                    LoginFail()
                End Try
            ElseIf (step_action = "check_login") Then
                If (ParseHTML.CheckByName(web, "input", "email") = True) Then
                    LoginFail()
                End If
            End If
        Else
            step_action = ""
            Debug.Print(infoLogin.Fullname & " - " & infoLogin.FacebookID)
            LoginSuccess()
        End If
    End Sub

End Class
